<?php  
//if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hello_controller extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		
		$temp = $this->session->userdata('is_logged_in');
		if(!isset($temp) || !$temp) exit('No direct script access allowed');
				
		$this->load->model('hello_model');
		$this->load->model('area_model');
	}


	function to_create_hello()
	{
		$d['status_create'] = true;
		$this->session->set_userdata($d);
			$data['title']="Post New Hello";
		$data['id']="new";
		
		$this->load->view('header',$data);
		$this->load->view('map',$data);

		/*
		$data['title']="Post New Hello";
		$data['id']="new";
		$data['area']=$this->area_model->ChangeQueryResultToArray($this->area_model->getAll());
		$this->load->view('header',$data);
		$this->load->view('create_hello_form',$data);
		$this->load->view('footer',$data);*/
	}
	function create_or_lucky()
	{
		$lat=$this->input->get('lat');
		$longi=$this->input->get('longi');
		if($this->session->userdata('status_create'))
		{
			//$data['status_create'] = false;
			//$this->session->set_userdata($data);
			$this->choose_place($lat, $longi);
		}
		else
		{
			$this->get_lucky_hello($lat, $longi);
		}
		
	}
	function choose_place($lat, $longi)
	{
		$data['lat']=$lat;
		$data['longi']=$longi;
		$data['title']="Post New Hello";
		$data['id']="";
		$data['area']=$this->area_model->ChangeQueryResultToArray($this->area_model->get_near($lat,$longi) );
		$this->load->view('header',$data);
		$this->load->view('choose_place_view',$data);
		$this->load->view('footer',$data);
	}
	function to_fill_form()
	{
		$data['title']="Post New Hello";
		$data['id']="";
		$data['areaid']=$this->input->get('areaid');
		$data['areaname']=$this->input->get('areaname');
		$this->load->view('header',$data);
		$this->load->view('create_hello_form',$data);
		$this->load->view('footer',$data);
	}

	function create_new_area()
	{
		// field name, error message, validation rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules('areaname', 'areaname', 'trim|required');

		if($this->form_validation->run() == FALSE)
		{
			$lat = $this->input->post("lat");
			$longi = $this->input->post("longi");
			$this->choose_place($lat, $longi);
		}
		else
		{
			$area = $this->area_model->create();
			redirect('hello_controller/to_fill_form?areaid='. $area->areaid .'&areaname='. $area->areaname.' ');
		}
	}
    function test_time($time)
    {
        if(strlen($time)!=5)
            return false;
        if($time[0]=='0' or $time[0]=='1')
        {
            if(!((int)substr($time,1,1)<=9 and (int)substr($time,1,1)>=0))
                return false;
        }
        else if($time[0]=='2')
        {
            if(!((int)substr($time,1,1)<=4 and (int)substr($time,1,1)>=0))
                return false;
        }
        else return false;
        
        if($time[2] != ':')  return false;
        
        if((int)substr($time,3,1) >=0 and (int)substr($time,3,1) <= 5)
        {
            if((int)substr($time,4,1) < 0 or (int)substr($time,4,1) > 9)
                return false;                
             return true;
        }
        else if($time[3]=='6')
        {
            if($time[4] != '0')
                return false;
              return true;
        }
        else return false;
        
    }
    function test_date($date)
    {
        if (preg_match("/^(\d{4})-(\d{2})-(\d{2})$/", $date, $matches)) {
        
            if (checkdate($matches[2], $matches[3], $matches[1])) 
            {
                return true;
            }
        }
        return false;
    }
	function create_hello()
	{
		// field name, error message, validation rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[4]');
		$this->form_validation->set_rules('location', 'Location', 'trim|required|min_length[1]|max_length[32]');
		//$this->form_validation->set_rules('time', 'Time', 'trim|required');
		//$this->form_validation->set_rules('date', 'Date', 'trim|required');
		$this->form_validation->set_rules('quota', 'Quota', 'trim|required|integer');
        $valid_time = $this->test_time($this->input->post('time'));
        $valid_date = $this->test_date($this->input->post('date'));
        
		if($this->form_validation->run() == FALSE or !$valid_time or !$valid_date)
		{
			$data['title']="Post New Hello";
			$data['id']="";
			$data['areaid']=$this->input->post('areaid');
			$data['areaname']=$this->input->post('areaname');
			$data['valid_time'] = $valid_time;
			$data['valid_date'] = $valid_date;
			$this->load->view('header',$data);
			$this->load->view('create_hello_form',$data);
			$this->load->view('footer',$data);
		}
		else
		{
			if( $query = $this->hello_model->create() )
			{
				redirect('feed_controller/index');
			}
			else
			{
				$data['title']="Post New Hello";
				$data['id']="";
				$data['areaid']=$this->input->post('areaid');
				$data['areaname']=$this->input->post('areaname');
				$this->load->view('header',$data);
				$this->load->view('create_hello_form',$data);
				$this->load->view('footer',$data);
			}
		}
	}

	function get_lucky_hello($lat, $longi)
	{
		$lucky = $this->hello_model->ChangeQueryResultToArray( $this->hello_model->get_lucky_hello($lat, $longi) );
		$luckyid = $lucky[0]->helloid;
		if(isset($luckyid))
			redirect('feed_controller/to_apply?helloID='.(string)($luckyid) );
		else
			redirect('feed_controller/index');
	}
	function my_waiting()
	{
		$data['title']="Hello World";
		$data['id']="feed";
		$data['rows'] = $this->hello_model->ChangeQueryResultToArray($this->hello_model->get_waiting_with_fullname());
		
		$this->load->view('header',$data);
		$this->load->view('feed_view', $data);
		$this->load->view('footer',$data);
	}

}


